<?php

namespace App\Job\YunXin;


use App\Utils\LoggerUtil;
use Hyperf\AsyncQueue\Job;
use Throwable;


/**
 * Note - 点对点普通自定义消息队列（触发位置包含自动打招呼、一键打招呼、主页停留、托管消息）
 * DateTime 2024/7/31 15:53
 * User Admin
 * Class YunXinCustomMsgJob
 * @package App\Job\YunXin
 */
class YunXinCustomMsgJob extends Job
{
    private mixed $params;

    public function __construct($params)
    {
        // 这里最好是普通数据，不要使用携带 IO 的对象，比如 PDO 对象
        $this->params = $params;
    }

    public function handle()
    {
        // TODO: Implement handle() method.
        $this->job($this->params);
    }

    private function job(mixed $origin)
    {
        try {
            $body = json_decode($origin, true);
            LoggerUtil::write('custom_msg_queue_job', $body);
            $fromUid = $body['fromUid'];
            $toUid = $body['toUid'];
            $message = $body['message'];

            if (empty($fromUid) || empty($toUid) || empty($message)) {
                throw new \Exception('数据不完整');
            }
            (new \App\Services\YunXinCloud\WyyClient())->sendCustomMsg($fromUid, $toUid, $message);
        } catch (Throwable $e) {
            LoggerUtil::write("custom_msg_queue_job", [
                "message" => $e->getMessage(),
                "file" => $e->getFile(),
                "line" => $e->getLine(),
            ]);
        }
    }
}